<?php

//add_action('template_redirect', 'auctionTheme_delete_drafts'); //wp_init - here
add_action('init', 'auctionTheme_close_auctions'); //wp_init - here

function auctionTheme_delete_drafts()
{

		$drft = array(
			'key' => 'is_draft',
			'value' => "1",
			'compare' => 'LIKE'
		);
		
		$tm = current_time('timestamp',0);
		if(empty($tm)) $tm = time();
		
		$drft_until = array(
			'key' => 'draft_until',
			'value' => $tm,
			'type' => 'numeric',
			'compare' => '<'
		);
		
		
	$args2 = array( 'posts_per_page' =>'-1', 'post_type' => 'auction', 'post_status' => array('draft','publish'), 'meta_query' => array($drft,$drft_until));
	$the_query = new WP_Query( $args2 );
	
	
		if($the_query->have_posts()):
		while ( $the_query->have_posts() ) : $the_query->the_post();
			
			wp_delete_post(get_the_ID());
		
		endwhile;
		endif;
	
}


function auctionTheme_close_auctions()
{
	global $wpdb;
		$closed = array(
			'key' => 'closed',
			'value' => "0",
			'compare' => '='
		);
		
		
		$ending = array(
			'key' => 'ending',
			'value' => current_time('timestamp',0),
			'type' => 'numeric',
			'compare' => '<'
		);
		
		
	$args2 = array( 'posts_per_page' =>'-1', 'post_type' => 'auction', 'post_status' => 'publish', 'meta_query' => array($closed, $ending));
	$the_query = new WP_Query( $args2 );
	
	
		if($the_query->have_posts()):
		while ( $the_query->have_posts() ) : $the_query->the_post();
			
			update_post_meta(get_the_ID(), 'closed',"1");
			update_post_meta(get_the_ID(), 'closed_date',current_time('timestamp',0));
			$pid = get_the_ID();
			
			$post = get_post($pid);
		
			$reverse = get_post_meta(get_the_ID(), 'reverse',true);
		
			if(0) //$reverse == "yes" || $reverse == "1")
			{
				// just close the auction and email to users maybe...				
				$s = "select distinct uid from ".$wpdb->prefix."auction_bids where pid='$pid'";
				$r = $wpdb->get_results($s);
				
				foreach($r as $row)
				{
					$uid = $row->uid;	
					
					$subject 	= sprintf(__("Auction closed: %s",'AuctionTheme'), $post->post_title);
					$message 	= sprintf(__("The auction <a href='%s'>%s</a> was just closed. A winner hasnt been chosen yet.", 'AuctionTheme'), 
					get_permalink($pid),$post->post_title );
					$user 		= get_userdata($uid);
					$email  	= $user->user_email;
					
					//AuctionTheme_send_email($email, $subject, $message);
					
					
				}
				
				//--- email to the owner as well ---
					
					$subject 	= sprintf(__("Your auction was closed: %s",'AuctionTheme'), $post->post_title);
					$message 	= sprintf(__("The auction <a href='%s'>%s</a> was just closed. Please choose a winner.",''),get_permalink($pid),$post->post_title );
					$user 		= get_userdata($post->post_author);
					$email  	= $user->user_email;
					
					//AuctionTheme_send_email($email, $subject, $message);
				
				//-------
				
			}
			else
			{
				// a little computation here, choose winner, inform people :P
				
				$s = "select * from ".$wpdb->prefix."auction_bids where pid='$pid' order by bid desc limit 1";
				$r = $wpdb->get_results($s);
				
				if(count($r) == 0)
				{
					
					AuctionTheme_send_email_on_no_win_owner($pid);
				
				}
				else
				{
					$row = $r[0];	
					$id = $row->id;
					
				
					update_post_meta($pid, 'closed',"1");
					update_post_meta($pid, 'closed_date', current_time('timestamp',0));
					
					$only_buy_now = get_post_meta($pid, 'only_buy_now', true);
					
					if($only_buy_now == "1"):
					
					
					else:
					
					$reserve = get_post_meta($pid, 'reserve', true);
					$reserve_ok = 1;
					
					if(!empty($reserve)){
						if($reserve <= $row->bid)  $reserve_ok = 1;
						else $reserve_ok = 2; // not met
					}
					
					if($reserve_ok == 1):
					
							$bidid = $id;
							add_post_meta($pid, 'winner', $row->uid);
							update_post_meta($pid, 'paid_user', "0");
							
							$tm = current_time('timestamp',0);
								
							$s = "update ".$wpdb->prefix."auction_bids set winner='1', date_choosen='$tm' where id='$id'";
							$wpdb->query($s);
							
							auctionTheme_prepare_rating($pid, $post->post_author, $row->uid);
							auctionTheme_prepare_rating($pid, $row->uid, $post->post_author);
							
							// send email to the winner -----
							AuctionTheme_send_email_on_win_to_bidder($pid, $row->uid);
							
							
							// send email to the owner -----							
							AuctionTheme_send_email_on_win_to_owner($pid, $row->uid, $bidid);

							
							//-----email to the other lower bidders-----
							
								global $wpdb;
								$s1 = "select distinct uid from ".$wpdb->prefix."auction_bids where id!='".$bidid."' AND pid='$pid'";
								$r1 = $wpdb->get_results($s1);
							
								foreach($r1 as $row1)
								{									
									AuctionTheme_send_email_on_win_to_loser($pid, $row1->uid);
								}
							
							//----------
						else:
						//------ reserve price is not met
							

							AuctionTheme_send_email_on_no_win_reserve_not_met_owner($Pid);
					
							global $wpdb;
							$s1 = "select distinct uid from ".$wpdb->prefix."auction_bids where pid='$pid'";
							$r1 = $wpdb->get_results($s1);
							
							foreach($r1 as $row1)
							{									
								AuctionTheme_send_email_on_no_win_reserve_not_met_loser($pid, $row1->uid);
							}
							
							
						endif;
						endif;
					
				}
				
			}
		
		endwhile;
		endif;
	
}


?>